using System.Collections;
using System.Collections.Generic;
using Entitas;
using UnityEngine;

/// <summary>
/// 滑动响应类
/// </summary>
public class SlideSystem : ReactiveSystem<InputEntity>
{
    private Contexts _contexts;

    public SlideSystem(Contexts contexts) : base(contexts.input)
    {
        _contexts = contexts;
    }

    protected override ICollector<InputEntity> GetTrigger(IContext<InputEntity> context)
    {
        return context.CreateCollector(InputMatcher.Slide);
    }

    protected override bool Filter(InputEntity entity)
    {
        return entity.hasSlide;
    }

    protected override void Execute(List<InputEntity> entities)
    {
        if (entities.Count == 1)
        {
            InputEntity entity = entities.SingleEntity();
            CustomVector2 pos = new CustomVector2(entity.slide.clickPos.x, entity.slide.clickPos.y);
            bool canMove = _contexts.game.GetEntitiesWithItemIndex(pos).SingleEntity().isMovable;

            if (canMove)
            {
                var nextPos = NextPos(entity);
                _contexts.input.ReplaceClick(nextPos.x, nextPos.y);
            }
        }
    }

    private CustomVector2 NextPos(InputEntity entity)
    {
        int x = entity.slide.clickPos.x;
        int y = entity.slide.clickPos.y;

        switch (entity.slide.SlideDirection)
        {
            case SlideDirection.LEFT:
                break;
            case SlideDirection.RIGHT:
                break;
            case SlideDirection.UP:
                break;
            case SlideDirection.DOWN:
                break;
        }

        x = Mathf.Clamp(x, 0, _contexts.game.gameBoard.columns);
        y = Mathf.Clamp(y, 0, _contexts.game.gameBoard.rows);

        return new CustomVector2();
    }
}